<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title">
        <h1>产业园区管理运营平台</h1>
      </div>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="phone">
          <el-input v-model="loginForm.phone" placeholder="手机号">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="userType" class="user-type-item">
          <el-radio-group v-model="loginForm.userType">
            <el-radio label="管理员">管理员</el-radio>
            <el-radio label="物业员工">物业员工</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-btn" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

export default {
  name: 'Login',
  components: {
    User,
    Lock
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const loginForm = reactive({
      phone: '',
      password: '',
      userType: '管理员'
    })
    
    const loginRules = {
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
      ],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      userType: [{ required: true, message: '请选择用户类型', trigger: 'change' }]
    }
    
    const loginFormRef = ref(null)
    const loading = ref(false)
    
    const handleLogin = () => {
      loginFormRef.value.validate(async valid => {
        if (valid) {
          loading.value = true
          try {
            await store.dispatch('login', {
              phone: loginForm.phone,
              password: loginForm.password,
              userType: loginForm.userType,
              employeeType: loginForm.employeeType
            })
            
            ElMessage.success('登录成功')
            
            // 根据用户类型跳转到不同的页面
            if (loginForm.userType === '管理员') {
              router.push('/admin')
            } else {
              router.push('/staff')
            }
          } catch (error) {
            console.error('登录失败:', error)
            // 更详细的错误日志
            if (error.response) {
              console.error('错误响应状态:', error.response.status)
              console.error('错误响应数据:', error.response.data)
              // 使用后端返回的具体错误消息
              if (error.response.data && error.response.data.message) {
                ElMessage.error(error.response.data.message)
              } else {
                ElMessage.error('登录失败，请检查手机号和密码')
              }
            } else if (error.request) {
              console.error('请求已发送但没有收到响应')
              ElMessage.error('网络连接问题，请稍后重试')
            } else {
              console.error('请求设置时出错:', error.message)
              ElMessage.error(error.message || '登录失败，请检查手机号和密码')
            }
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    // 如果已经登录，根据用户类型跳转
    onMounted(() => {
      if (store.getters.isAuthenticated) {
        if (store.getters.isAdmin) {
          router.push('/admin')
        } else {
          router.push('/staff')
        }
      }
    })
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: url("@/assets/images/login.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
}

.login-box {
  width: 420px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  z-index: 1;
}

.title {
  margin-bottom: 40px;
  text-align: center;
  transform: translateY(-15px);
}

.title h1 {
  font-size: 32px;
  font-family: "Microsoft YaHei", "微软雅黑", "PingFang SC", "Hiragino Sans GB", sans-serif;
  color: #2c5282;
  margin: 0;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  white-space: nowrap;
}

.login-form {
  width: 100%;
}

.user-type-item {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

:deep(.user-type-item .el-form-item__content) {
  justify-content: center;
  margin-left: 0 !important;
  width: 100%;
}

:deep(.user-type-item .el-radio-group) {
  display: flex;
  justify-content: center;
  gap: 100px;
  width: auto;
}

:deep(.user-type-item .el-radio) {
  margin-right: 0;
}

:deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(44, 82, 130, 0.2);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  padding: 0 15px;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(44, 82, 130, 0.4);
}

:deep(.el-input__inner) {
  height: 42px;
}

:deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #2c5282;
  border-color: #2c5282;
}

:deep(.el-radio__label) {
  color: #2c5282;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  background-color: #2c5282;
  border-color: #2c5282;
  border-radius: 8px;
  margin-top: 10px;
  transition: all 0.3s;
}

.login-btn:hover {
  background-color: #3a6da5;
  border-color: #3a6da5;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(44, 82, 130, 0.3);
}

@media (max-width: 768px) {
  .login-box {
    width: 90%;
    max-width: 380px;
    padding: 30px 20px;
  }
  
  .title h1 {
    font-size: 28px;
    white-space: normal;
  }
}
</style> 